package ru.ruappmaker.calendar;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.flurry.android.FlurryAgent;
import com.google.ads.*;
import com.viewpagerindicator.TabPageIndicator;
import ru.ruappmaker.calendar.R;

import android.os.Bundle;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends CalendarActivity implements
		View.OnClickListener {
	String[] months;
	private int selectedQuarter;

	private OnPageChangeListener pageListener = new OnPageChangeListener() {
		public void onPageSelected(int arg0) {
			selectedQuarter = arg0;
		 getSupportActionBar().setTitle(String.valueOf(selectedQuarter+1) + " квартал "
					+ String.valueOf(year())
					+ " года");
		 FlurryAgent.logEvent("Quarter view: quarter changed to " + String.valueOf(selectedQuarter+1));
		}

		public void onPageScrollStateChanged(int arg0) {
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}
	};

	private Calendar calendar;
	private TabPageIndicator pageIndicator;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH);
		months = getResources().getStringArray(R.array.monthNames);
		selectedQuarter =  month / 3;
		Intent instance = getIntent();
		Bundle extras = instance.getExtras();

		if (extras != null) {
			_year = extras.getInt("year");
			selectedQuarter = extras.getInt("quarter");
		}
		
		InitViewPager();
	}

	@Override
	protected void InitViewPager() {
		FillDataArrays();
		
		LayoutInflater inflater = LayoutInflater.from(this);
		List<View> pages = new ArrayList<View>();

		for (int i = 0; i < 4; i++) {
			View quarter = inflater.inflate(R.layout.activity_main, null);
			FillQuarter(quarter, i);
			pages.add(quarter);
		}
		
		setContentView(R.layout.pager_layout);
		CalendarPager pagerAdapter = new CalendarPager(pages);
		ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
		viewPager.setAdapter(pagerAdapter);
		viewPager.setPageMargin(1);
		viewPager.setPageMarginDrawable(R.color.screen_split);
		 pageIndicator = (TabPageIndicator)findViewById(R.id.page_indicator);
		pageIndicator.setOnPageChangeListener(pageListener);
		pageIndicator.setViewPager(viewPager);
		pageIndicator.setCurrentItem(selectedQuarter);

		setTitle(String.valueOf(selectedQuarter+1) + " квартал "
				+ String.valueOf(year())
				+ " года");
		
		 AdView adView = (AdView)this.findViewById(R.id.adView);
		 AdRequest adRequest = new AdRequest();
		 adView.loadAd(adRequest);
	}

	private void FillQuarter(View quarter, int number) {

		LinearLayout calTable = (LinearLayout) quarter
				.findViewById(R.id.main_calendar);
        int year = year();
		createCalendar(calTable, number * 3 + 1, year, false);
		calTable.setOnClickListener(this);
		calTable = (LinearLayout) quarter.findViewById(R.id.main_calendar2);
		createCalendar(calTable, number * 3 + 2, year, false);
		calTable.setOnClickListener(this);
		calTable = (LinearLayout) quarter.findViewById(R.id.main_calendar3);
		createCalendar(calTable, number * 3 + 3, year, false);
		calTable.setOnClickListener(this);

		((TextView) quarter.findViewById(R.id.month1))
				.setText(months[number * 3]);
		((TextView) quarter.findViewById(R.id.month2))
				.setText(months[number * 3 + 1]);
		((TextView) quarter.findViewById(R.id.month3))
				.setText(months[number * 3 + 2]);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.main_calendar:
			showMonth(selectedQuarter * 3 + 1);
			break;
		case R.id.main_calendar2:
			showMonth(selectedQuarter * 3 + 2);
			break;
		case R.id.main_calendar3:
			showMonth(selectedQuarter * 3 + 3);
			break;
		}
	}

	private void showMonth(int number) {

		Intent newIntent = new Intent(getApplicationContext(), MonthView.class);
		newIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		newIntent.putExtra("month", number);
		newIntent.putExtra("year", year());
		this.startActivity(newIntent);
	}
	
	@Override
	protected void RestartThis() {
		Intent i = new Intent(this, getSubClassObject().getClass());
		i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		i.putExtra("quarter", selectedQuarter);
		i.putExtra("year", year());
		this.finish();
		this.startActivity(i);	
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		SharedPreferences settings = getSharedPreferences("RuappMakerCal", 0);
		String savedTheme = settings.getString("colorSchema","Blue");
		TypedValue outValue = new TypedValue();
		getTheme().resolveAttribute(R.attr.themeName, outValue, true);
		if (!savedTheme.equals(outValue.string.toString())) {
			RestartThis();
		}
	}

}
